package com.feifan.provider.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.feifan.provider.rabbitmq.QueueEnum.QUEUE_TTL_ORDER_CANCEL;


/**
 * @program: FinalProject
 * @author: Xiaofan
 * @createTime: 2021-08-11 15:22
 * @description: Functions of this class is
 **/
@Component
public class CancelOrderSender {
    private static Logger logger= LoggerFactory.getLogger(CancelOrderSender.class);

    @Autowired
    private AmqpTemplate amqpTemplate;//用来专门操作mq

    public void sendMessage(Long orderId,final Long delayTime){
        //开始给延迟队列发送消息
        amqpTemplate.convertAndSend(QUEUE_TTL_ORDER_CANCEL.getExchange(), QUEUE_TTL_ORDER_CANCEL.getRouteKey(),orderId, new MessagePostProcessor() {
            @Override//发送消息
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(delayTime.toString());//给消息设置延迟
                return message;
            }
        });
        logger.info("send orderId{}"+orderId);//对日志进行写
    }

}